package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

public class BandwidthAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);
        if(data.getTriggerStrategy() != null) {
            // 初始化加载页面时不校验，跳过初次加载时的属性 set 触发
            return;
        }
        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        String serviceOfferId = data.getServiceOfferId();
        if ("2826".equals(serviceOfferId)) {
            // 产品变更 校验升速、降速变更
            List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SERVICEOFFERID");
            String originValueName = getValueName(attr, attr.getOriginValue());
            String currentValueName = getValueName(attr, value);
            if(StringUtils.isBlank(originValueName) || StringUtils.isBlank(currentValueName) || Objects.isNull(serviceOfferIdList)){
                return;
            }
            //割接数据名称不是数字+单位时不校验
            String originStr = originValueName.replaceAll("[^0-9\\.]", "");
            String currentStr = currentValueName.replaceAll("[^0-9\\.]", "");
            if(KiteStringUtils.isAnyEmpty(originStr,currentStr)){
                return;
            }
            int originUnit = originValueName.endsWith("G") ? 1024 : 1;
            int currentUnit = currentValueName.endsWith("G") ? 1024 : 1;
            double current = Double.parseDouble(currentStr) * currentUnit;
            double origin = Double.parseDouble(originStr) * originUnit;
            if (serviceOfferIdList.contains("1020")){
                // 升速
                if(origin > current) {
                    BssException.throwOut("", "","升速带宽新值不能小于原始带宽值");
                }
            } else if (serviceOfferIdList.contains("1021")){
                // 降速
                if(origin < current) {
                    BssException.throwOut("", "","降速带宽新值不能大于原始带宽值");
                }
            }
        }
    }

    private static String getValueName (KiteAttr attr, String OriginValue){
        for(KiteAttrValue kiteAttrValue : attr.getAttrValues()) {
            if(kiteAttrValue.getAttrValue().equalsIgnoreCase(OriginValue)) {
                return kiteAttrValue.getAttrValueName();
            }
        }
        return null;
    }

}
